package summer.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution257 {
    List<String> res = new ArrayList<>();

    List<Integer> tmp = new LinkedList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return res;
        backtracking(root);
        return res;
    }

    public void backtracking(TreeNode root) {
        tmp.add(root.val);
        if (root.left == null && root.right == null) {
            StringBuilder sb = new StringBuilder();
            for (Integer i : tmp) {
                sb.append(i).append("->");
            }
            res.add(sb.substring(0, sb.length() - 2));
            return;
        }
        if (root.left != null) {
            backtracking(root.left);
            tmp.remove(tmp.size() - 1);
        }
        if (root.right != null) {
            backtracking(root.right);
            tmp.remove(tmp.size() - 1);
        }
    }
}
